<template>
  <div class="login">
    <h1>新商城商铺后台管理系统</h1>
    <div class="content">
      <h3>用户登录/User login</h3>
      <el-form :model="form.data" :rules="form.rules" ref="formRef">
        <el-form-item prop="username">
          <el-input
            v-model="form.data.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.data.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="form.submitForm()"
            width="100%"
            style="display:block;"
            >登录</el-button
          >
          <!-- <el-button @click="form.resetForm()">重置</el-button> -->
        </el-form-item>
        <el-form-item>
          <span class="reset" @click="form.resetForm()">清空表单</span>
        </el-form-item>
      </el-form>
      <p class="error">{{ error }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      error: '',
      form: {
        data: {
          username: 'admin',
          password: '123456'
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            {
              min: 2,
              max: 10,
              message: '长度在 2 到 10 个字符',
              trigger: 'blur'
            }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 6,
              max: 20,
              message: '长度在 6 到 20 个字符',
              trigger: 'blur'
            }
          ]
        },
        submitForm: () => {
          this.$refs.formRef.validate(async validate => {
            if (!validate) {
              this.$message.error('输入用户名和密码')
              return false
            }
            if (
              this.form.data.username === 'admin' &&
              this.form.data.password === '123456'
            ) {
              const { data: res } = await this.$http.get('/login')
              window.sessionStorage.setItem('LOGIN_TOKEN', res.result)
              this.$router.push('/')
              this.$message.success('登录成功！')
            } else {
              this.$message.error('用户名密码错误')
            }
            // this.$router.push('/home')
          })
        },
        resetForm: () => {
          this.$refs.formRef.resetFields()
        }
      }
    }
  },
  methods: {},
  created() {}
}
</script>

<style lang="less" scoped>
.login {
  position: absolute;
  background: url('../assets/images/login-bgc.jpg') no-repeat center;
  background-size: cover;
  height: 100%;
  width: 100%;
  h1 {
    color: #fff;
    position: fixed;
    top: 60px;
    left: 225px;
  }
  .content {
    background: #212849;
    width: 300px;
    height: 350px;
    position: absolute;
    right: 200px;
    top: 150px;
    border-radius: 3px;
    padding: 20px;
    h3 {
      color: #fff;
      text-align: center;
      padding: 10px 0;
    }
    .reset {
      color: #fff;
      position: absolute;
      right: 10px;
      font-size: 16px;
      text-decoration: underline;
    }
    .el-button--primary {
      width: 100%;
      background-color: #1ac2ad;
      border: none;
    }
  }
  .error {
    position: absolute;
    bottom: -46px;
    left: 50%;
    transform: translateX(-50%);
    color: #1ac2ad;
  }
}
</style>
